// GameBuilder.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GameBuilder.h"
#include "GameKit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int Build(LPCTSTR szGameFile, LPCTSTR szOutput);

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = -1;

	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		if(argc < 3)
		{
			printf("Usage: <Input Game File> <Output Folder>\n");
		}
		else
		{
			LPCTSTR szInput = argv[1];
			LPCTSTR szOutput = argv[2];

			return Build(szInput, szOutput);
		}
	}

	return nRetCode;
}

int Build(LPCTSTR szInputFile, LPCTSTR szOutput)
{
	try
	{
		CFile file(szInputFile, CFile::modeRead);
		CArchive ar(&file, CArchive::load);

		CGameKit GameKit;

		if(GameKit.Load(ar))
		{
			GameKit.Build(szOutput);
		}

		return 0;
	}
	catch(CException* e)
	{
		e->ReportError();
		e->Delete();
		return -1;
	}
}
